/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.abc;

import java.util.List;
import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.ListBox;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.TextField;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.list.DefaultListModel;
import org.sapra.echo.Helpers;
import org.sapra.dao.Departamento;
import org.sapra.dao.Empresa;
import org.sapra.echo.wAviso;
import org.sapra.logon.dLogin;
import org.sapra.main.vPrincipal;
/**
 *
 * @author cobain
 */
public class vDepartamento extends nextapp.echo2.app.ContentPane {

    Column mParent=null;
    private SelectField mListaDep;
    private ListBox mlistaEmp;
    private TextField nEmpresa;
    private Button guardar;
    private Button borrar;
    private Button nuevo;
    private Button editar;
    private Button cancelar;
    private TextField nDepto;
    //private int counter = 1;
    private Row row6;

    public final static int EDITAR = 2;
    public final static int NUEVO = 1;
    public static int accion;
    private vPrincipal princ;
    Departamento mdepto;
    Empresa emp=new Empresa();


        public vDepartamento(vPrincipal princ) {
        super();
        this.princ=princ;
        initComponents();
    }

    public vDepartamento(Column parent){
        mParent = parent;
        initComponents();
    }

    private void initComponents() {
        Column col1;


        if(mParent != null) col1 = mParent;
        else{
            col1 = Helpers.Column("Default", new Extent(5),this);
            Row x=princ.getMenu();
            x.setAlignment(Alignment.ALIGN_CENTER);
            col1.add(x);
        }
        Row row1 = Helpers.Row("Default", new Extent(5), col1);
        Helpers.Label("SELECCIONE UN DEPARTAMENTO", new Color(0x011dac), row1);
        row1.setAlignment(Alignment.ALIGN_CENTER);
        
        Row row4 = Helpers.Row("Default", new Extent(5), col1);
        row4.setAlignment(Alignment.ALIGN_CENTER);
        Helpers.Label("EMPRESA:", Color.BLACK, row4);
        nEmpresa = Helpers.Text("", row4);
        nEmpresa.setWidth(new Extent(500));


        Row row2 = Helpers.Row("Default", new Extent(5), col1);
        Helpers.Label("DEPARTAMENTO:", Color.BLACK, row2);
        row2.setAlignment(Alignment.ALIGN_CENTER);
        Row row3 = Helpers.Row("Default", new Extent(5), col1);
        row3.setAlignment(Alignment.ALIGN_CENTER);
        mListaDep= Helpers.SelectField(col1,new ActionListener(){public void actionPerformed(ActionEvent e) {Seleccion(e);}});
        row3.add(mListaDep);
        mListaDep.setWidth(new Extent(450));

        Row row5 = Helpers.Row("Default", new Extent(5), col1);
        row5.setAlignment(Alignment.ALIGN_CENTER);
        nuevo=Helpers.Button("NUEVO", row5,new ActionListener(){public void actionPerformed(ActionEvent e) {MenuNuevo(e);}});
        editar=Helpers.Button("EDITAR", row5,new ActionListener(){public void actionPerformed(ActionEvent e) {MenuEditar(e);}});
        borrar=Helpers.Button("BORRAR", row5,new ActionListener(){public void actionPerformed(ActionEvent e) {MenuBorrar(e);}});

        row6 = Helpers.Row("Default", new Extent(5), col1);
        row6.setAlignment(Alignment.ALIGN_CENTER);
        Helpers.Label("NOMBRE DEL DEPARTAMENTO:", Color.BLACK, row6);
        nDepto=Helpers.Text("", row6);
        guardar=Helpers.Button("GUARDAR", row6,new ActionListener(){public void actionPerformed(ActionEvent e) {MenuGuardar(e);}});
        cancelar=Helpers.Button("CANCELAR", row6,new ActionListener(){public void actionPerformed(ActionEvent e) {MenuCancelar(e);}});
        row6.setVisible(false);
       
        FirstTime();
    }

        void FirstTime(){
            UpdateView();
    }

        void UpdateView(){
        //Actualiza la Lista
        List<Departamento> deptos= dDepartamento.getDepartamentos(dLogin.getEmpresa());
        mListaDep.setModel(new DefaultListModel(deptos.toArray()));
        nEmpresa.setText( dLogin.getEmpresa().getRazon_empresa());
        nEmpresa.setEnabled(false);
        mdepto = null;
    }

        void Seleccion(ActionEvent e){
        mdepto = (Departamento) mListaDep.getSelectedItem();
        if(mdepto == null) {
                add(new wAviso("ERROR","ERROR EN LA APLICACION"));
                return;
            }
     }
        
     void MenuNuevo(ActionEvent e) {
         row6.setVisible(true);
         accion=NUEVO;
         mdepto=null;
         nDepto.setText("");
    }
        void MenuGuardar(ActionEvent e){
         if(accion==NUEVO){
            String nombre = nDepto.getText();
            dDepartamento.NuevoDepto(nombre);
            row6.setVisible(false);
            UpdateView();}
         if(accion==EDITAR){
           mdepto = (Departamento) mListaDep.getSelectedItem();
           row6.setVisible(false);
           String nombre=nDepto.getText();
           /*if (dDepartamento.editarDepartamento(mdepto,nombre) == 0){
            add(new wAviso("ERROR","NO SE PUEDE HACER CAMBIOS EN EL DEPARTAMENTO"));
        }*/
        dDepartamento.editarDepartamento(mdepto,nombre);
        nDepto.setText(null);
        UpdateView();

         }

    }
        void MenuBorrar(ActionEvent e){
        mdepto = (Departamento) mListaDep.getSelectedItem();
        if(mdepto == null)return;
        int a=dDepartamento.borrarDepto(mdepto);
        if (dDepartamento.borrarDepto(mdepto) !=0){
            add(new wAviso("ERROR","NO SE PUEDE BORRAR EL DEPARTAMENTO,TIENE PRACTICANTES ASIGNADOS"));
        }else if(dDepartamento.borrarDepto(mdepto) ==0){
            add(new wAviso("Exito","DEPARTAMENTO ELIMINADO CORRECTAMENTE"));
        }
        nDepto.setText("");
        UpdateView();

    }

        void MenuEditar(ActionEvent e){
            if(mdepto == null){
                add(new wAviso("ERROR","FAVOR DE SELECCIONAR UN DEPARTAMENTO"));
                return;
            }
        row6.setVisible(true);
        accion=EDITAR;
        nDepto.setText(mdepto.getNombre());
        }

        void MenuCancelar(ActionEvent e){
             UpdateView();
             row6.setVisible(false);
        }

}